import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Param,
  Body,
  UsePipes,
  ValidationPipe,
} from '@nestjs/common';
import { RoleService } from '../services/role.service';
import { Role } from '../models/role.entity';

@Controller('roles')
export class RoleController {
  constructor(private readonly roleService: RoleService) {}

  @Get()
  async getRoles(): Promise<Role[]> {
    return this.roleService.findAll();
  }

  @Get(':id')
  async getRole(@Param('id') id: string): Promise<Role | null> {
    return this.roleService.findOne(Number(id));
  }

  @Post()
  @UsePipes(new ValidationPipe())
  async createRole(@Body() roleData: Partial<Role>): Promise<Role> {
    return this.roleService.create(roleData);
  }

  @Put(':id')
  @UsePipes(new ValidationPipe())
  async updateRole(
    @Param('id') id: string,
    @Body() roleData: Partial<Role>,
  ): Promise<Role | null> {
    return this.roleService.update(Number(id), roleData);
  }

  @Delete(':id')
  async deleteRole(@Param('id') id: string): Promise<Role | null> {
    return this.roleService.remove(Number(id));
  }
}
